<template>
  <el-dialog
    :title="`为【${title}】分配权限`"
    width="700px"
    :visible.sync="isShow"
    @close="closeEvent"
  >
    <el-tree
      ref="tree"
      check-strictly
      :data="list"
      node-key="id"
      :default-checked-keys="permIds"
      show-checkbox
      default-expand-all
      :props="{ label: 'name' }"
    />
    <template #footer>
      <div class="tc">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="isShow = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission'
import { sysRoleId, sysRoleAssignPrem } from '@/api/setting'

export default {
  data() {
    return {
      title: '',
      isShow: false,
      list: [],
      permIds: [],
      id: ''
    }
  },
  methods: {
    async getData() {
      if (this.list.length === 0) {
        const res = await sysPermission()
        this.list = this.changeData(res.data, '0')
        console.log('权限数据列表', res)
      }
    },
    changeData(arr, pid) {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 获取角色详情
    async getRoleInfo(id) {
      this.id = id
      const res = await sysRoleId(id)
      this.permIds = res.data.permIds
    },
    // 关闭弹框处理
    closeEvent() {
      this.$refs.tree.setCheckedKeys([])
    },
    // 确认点击
    async submit() {
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配权限成功')
      this.isShow = false
    }
  }
}
</script>
<style lang="scss" scoped>
.tc {
  text-align: center;
}
</style>
